<?php
require_once("mysqlUser/mysql.php");
require_once("gameObjects/mainWeapon.php");
require_once("gameObjects/secWeapon.php");
require_once("gameObjects/robot.php");
require_once("gameObjects/mission.php");

class user
{
	public static $table = "user";
	
	public static function updatePeerID($uid,$peerID)
	{
		$command = "update ".user::$table." set peer='$peerID' where uid=$uid ";
		mysql::run($command);
	}
	
	public static function getPeerID($uid)
	{
		$command = "select peer from ".user::$table." where uid=$uid ";
		$result = mysql::run($command);
		$data = mysql_fetch_array($result);
		
		return $data["peer"];
	}
	
	public static function generateUser($uname,$password)
	{
		$command = "select uid from ".user::$table." where uname='$uname' ";
		$result = mysql::run($command);
		$data = mysql_fetch_array($result);
		
		if(count($data)==1)
		{
			$command =  "INSERT INTO ".user::$table." VALUES ( NULL,'','$uname','$password','',0,1,0,0,0,0,0,0)"; 
			mysql::run($command);
			
			$command = "select uid from ".user::$table." where uname='$uname' AND password = '$password' ";
			$result =  mysql::run($command);
			$data = mysql_fetch_array($result);
			$uid = $data["uid"];
			
			return $uid;
		}else{
			return -1;
		}
	}
	
	public static function login($uname,$password)
	{
		$command = "select uid from user where uname='$uname' AND password = '$password' ";
		$result =  mysql::run($command);
		$data = mysql_fetch_array($result);

		if(count($data)==1)
		{
			return -1; 
		}else
		{
			return $data["uid"];
		}
	}
	
	public static function distroyUser($uid)
	{
		$command = "delete from ".user::$table." where uid=$uid";
		mysql::run($command);
			
		$command = "delete from ".robot::$table." where uid=$uid";
		mysql::run($command);
			
		$command = "delete from ".mainWeapon::$table." where uid=$uid";
		mysql::run($command);
		
		$command = "delete from ".secWeapon::$table." where uid=$uid";
		mysql::run($command);
		
		$command = "delete from ".mission::$table." where uid=$uid";
		mysql::run($command);
	}
	
	//////////////////////////////////////////
	
	private $uid;
	private $password;
	private $uname;
	private $niname;
	private $vip;
	private $level;
	private $exp;
	private $title;
	private $money;
	private $avi;
	private $acc;
	private $crit;
	private $missions;
	////////////////////////////////////////
    public function __set($name, $value) 
    {
        if ( isset( $this->$name ) )
        {       	
			
			if($name=='password' || $name=='uname' || $name=='niname')
			{
				$command = "update ".user::$table." set $name='$value' where uid=$this->uid ";
			}else if($name=='missions'){}
			else
			{
				$command = "update ".user::$table." set $name=$value where uid=$this->uid ";
			}
			mysql::run($command);
        }
		
        $this->$name = $value;
    }
	
    public function __get($name) 
    {
        if( isset( $this->$name ) ) 
        {      
			if($name == 'vip'||$name== 'level'||$name== 'exp'||$name== 'title'||$name== 'money'||$name== 'avi'||$name== 'acc'||$name== 'crit')
			{
				$command = "select * from ".user::$table." where uid=$this->uid";
				$result =  mysql::run($command);
				$vals=mysql_fetch_array($result);
				$this->$name = $vals[$name];
			}	
			
			return $this->$name;
			
        } else {
			trigger_error( $name . ' variables undefined',  E_USER_NOTICE );
        }
    }
    ////////////////////////////////////////
	/**
	 * 用户
	 * @param mixed $uid 用户id
	 */	
	public function user($uid)
	{
		$this->uid = $uid;
		
		$command = "select * from ".user::$table." where uid=$uid";
  		$result =  mysql::run($command);
    	$data=mysql_fetch_array($result);
    	
    	$this->uname = $data["uname"];
    	$this->password = $data["password"];
		$this->niname = $data["niname"];
		$this->vip = $data["vip"];
		$this->level = $data["level"];
		$this->exp = $data["exp"];
		$this->title = $data["title"];
		$this->money = $data["money"];
		$this->avi = $data["avi"];
		$this->acc = $data["acc"];
		$this->crit = $data["crit"];
		
		$command = "select * from missions where uid=$uid";
		$result =  mysql::run($command);
		while($vals=mysql_fetch_array($result))
		{
			$this->missions[]=new mission($vals['pid']);
		}
	}
	
	/**
	 * 接任务
	 */	
	public function getMission($mid)
	{
		foreach($this->missions as $mission)
		{
			if($mission->mid == $mid)
			{
				if($mission->canget)$mission->isgot = 1;
					
				break;
			}
		}
	}
	
	/**
	 * 交任务
	 */	
	public function deliverMission($mid)
	{
		foreach($this->missions as $mission)
		{
			if($mission->mid == $mid)
			{
				if($mission->reach)$mission->complete = 1;
				
				break;
			}
		}
	}
	
	/**
	 *取得玩家的道具列表
	 * @return 
	 */	
	public function getItems()
	{
		//取列表....
		$itemList;
		
		$stHouse = new storeHouse($this->uid);
		
		foreach($stHouse->robotParts as $part)
		{
			$itemList[]=array("id"=>10000+$part->id,"inc"=>$part->inc);
		}
		
		foreach($stHouse->mainWeaponParts as $part)
		{
			$itemList[]=array("id"=>20000+$part->id,"inc"=>$part->inc);
		}
		
		foreach($stHouse->secWeaponParts as $part)
		{
			$itemList[]=array("id"=>30000+$part->id,"inc"=>$part->inc);
		}
		
		return $itemList;
	}
	
}

?>